<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<div id='stockProductTable_win' style='display:none'></div>
<div id='stockProductTable_html' style='display:none'></div>
<script>
function f_addStockProduct(_afterConfirm){
  var storeStock=new Ext.data.Store({//仓库数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/stock/findAllStocks.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'stockNo'},
      {name:'stockName'}
    ]) 
  });
  storeStock.load();
  
  var comboStock=new Ext.form.ComboBox({//仓库下拉框
    store:storeStock,
    fieldLabel:'仓库',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'stockNo',
    displayField:'stockName',
    readOnly:true
  });
  
  var storeLocation=new Ext.data.Store({//库位数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/stock/findLocationsByStock.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'locationNo'},
      {name:'locationName'}
    ])
  });
  var comboLocation=new Ext.form.ComboBox({//库位下拉框
    store:storeLocation,
    fieldLabel:'库位',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'locationNo',
    displayField:'locationName',
    readOnly:true
  });
  
  
  
  comboStock.on('select',function(comboBox){
    var stockNo=comboBox.getValue();
    storeLocation.load({params:{'stockNo':stockNo}});
  });
  
  var htmlDiv=Ext.get("stockProductTable_html").dom;
  var stockProductTable='V_STOCK_PRODUCT_DETAIL';
  comboLocation.on('select',function(comboBox){
    var locationNo=comboBox.getValue();
    globalScope._model({tableName:stockProductTable,LOCAL_NO:locationNo},function(responseText){
	   htmlDiv.innerHTML=responseText;
	   var span=Ext.get(stockProductTable+"_columns").dom;
	   var columns=Ext.decode(span.innerText);
	   var stockProductGrid= new Ext.quick.QuickGrid(stockProductTable, {
	     id:'c_grid',
	     collapsible:true,
	     split:true,
	     stripeRows: true, 
	     enableDragDrop:false,
	     title:'grid',
	     clicksToEdit:1,
	     'remove':true,
	     height:g_height*85,
	     'smConfig':{}
	   },columns);
	   
	   stockProductGrid.on('afteredit',function(e){
	     var r=e.record;
	     var v1=parseFloat(e.value);
	     var v2=parseFloat(r.get('STOCK_NUM'));
	     if(v1>v2){
	       Ext.Msg.alert('<fmt:message key="label.warn"/>','出库数量('+v1+')不能大于库存数量('+v2+')');
	       r.reject();
	     }
	   });	   
	   
	   globalScope.grid.add(stockProductTable,stockProductGrid);
	   
	   var p=Ext.getCmp('c_product');
	   p.remove('c_grid');
	   p.add(stockProductGrid);
	   p.doLayout();
   });  
  });

  var stockLocationForm=new Ext.form.FormPanel({
    region:'north',
    labelAlign:'right',
    labelWidth:60,
    frame:true,
    split:true,
    collapsible:true,
    title: "detail",
    width:650,
     height:g_height*0.2,
    items:[{
      layout:'column',
      items:[{
          columnWidth:1/2,
          layout:'form',
          items:[comboStock]
        },{
          columnWidth:1/2,
          layout:'form',
          items:[comboLocation]
        }
      ]
    }]
  });
  
  	  Ext.get('stockProductTable_win').dom.innerHTML=""; 
	  var selectStockProductWin= new Ext.Window({
	    id:'c_win2',
	    el:'stockProductTable_win',
	    layout:'border',
        title:'选择库存产品',
	    width:1000,
	    height:g_height*1.2,
	    closeAction:'hide',
	    buttonAlign:'center',
	    items: [stockLocationForm,{id:'c_product',region:'center',layout:'fit',items:[{id:'c_grid'}] }],
	    buttons: [{
		        text:'<fmt:message key="button.conf"/>',
		        handler:function(){
                    var m=globalScope.grid.get(stockProductTable).getStore().getModifiedRecords();     
				    if(m.length==0){
				      Ext.Msg.confirm('title','没有选择库存产品,确认吗',function(btn){
                        if(btn=='yes'){
                          selectStockProductWin.hide();
                        }
                      });
                      return;
				    }
				    if(typeof(_afterConfirm)=='function'){
				      _afterConfirm(m);
				    }	
				    selectStockProductWin.hide();
	            }
		      },{
		        text:'<fmt:message key="button.close"/>',
		        handler:function(){selectStockProductWin.hide();}
		     }]
		});  
	   selectStockProductWin.render(document.body);  
	   selectStockProductWin.show(); 	 
}

</script>
